package com.dzt.cloudreader.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.dzt.cloudreader.R;
import com.dzt.cloudreader.activity.GalleryActivity;
import com.dzt.cloudreader.adapter.WelfareAdapter;
import com.dzt.cloudreader.adapter.recycler.RecyclerAdapter;
import com.dzt.cloudreader.base.LoadingFragment;
import com.dzt.cloudreader.model.GankIoDataBean;
import com.dzt.cloudreader.network.GankIoDataPresenter;
import com.dzt.cloudreader.network.GankIoDataView;
import com.dzt.cloudreader.utils.JLogKit;
import com.dzt.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by M02323 on 2017/12/15.
 */

public class WelfareFragment extends LoadingFragment {
	@BindView(R.id.xrv_welfare)
	XRecyclerView recyclerView;
	private WelfareAdapter welfareAdapter;
	private List<GankIoDataBean.ResultsBean> resultsBeen = new ArrayList<>();
	private GankIoDataPresenter presenter;
	private ArrayList<String> imgList = new ArrayList<>();
	private int mPage = 1;

	@Override
	protected int getLayoutId() {
		return R.layout.fragment_welfare;
	}

	@Override
	protected void initData() {
		welfareAdapter = new WelfareAdapter(context, resultsBeen, R.layout.item_welfare);
		welfareAdapter.setOnItemClickListener(new RecyclerAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(View view, int position) {
				Bundle bundle = new Bundle();
				bundle.putInt("selet", 2);// 2,大图显示当前页数，1,头像，不显示页数
				position--;
				bundle.putInt("code", position);//第几张
				bundle.putStringArrayList("imageuri", imgList);
				Intent intent = new Intent(getContext(), GalleryActivity.class);
				intent.putExtras(bundle);
				startActivity(GalleryActivity.class, intent);
			}
		});
		initRecyclerView();
		presenter = new GankIoDataPresenter(context);
		presenter.onCreate();
		presenter.attachView(dataView);
		loadWelfareData();
	}

	private void initRecyclerView() {
		recyclerView.setPullRefreshEnabled(false);
		//recyclerView.setLoadingMoreEnabled(false);
		recyclerView.clearHeader();
		recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
			@Override
			public void onRefresh() {

			}

			@Override
			public void onLoadMore() {
				mPage++;
				loadWelfareData();
			}
		});
		recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
		// 需加，不然滑动不流畅
		recyclerView.setNestedScrollingEnabled(false);
		recyclerView.setHasFixedSize(false);
		recyclerView.setItemAnimator(new DefaultItemAnimator());
		recyclerView.setAdapter(welfareAdapter);
	}

	private void loadWelfareData() {
		JLogKit.getInstance().i("page = " + mPage);
		presenter.getGankIoData("福利", 10, mPage);
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		if(presenter != null){
			presenter.onDestroy();
		}
	}

	GankIoDataView dataView = new GankIoDataView() {
		@Override
		public void onSuccess(GankIoDataBean gank) {
			showContentView();
			if (gank != null && gank.getResults() != null && gank.getResults().size() > 0) {
				JLogKit.getInstance().i("success size = " + gank.getResults().size());
				recyclerView.refreshComplete();
				resultsBeen.addAll(gank.getResults());
				welfareAdapter.notifyDataSetChanged();
				for (GankIoDataBean.ResultsBean bean : gank.getResults()) {
					imgList.add(bean.getUrl());
				}
			} else {
				JLogKit.getInstance().i("success no more");
				recyclerView.noMoreLoading();
			}
		}

		@Override
		public void onError(String result) {
			recyclerView.refreshComplete();
		}
	};
}
